import chatApiHttp from './chatApiHttp'
import axios from 'axios'
import Config from './Config'
import Utils from "../utils/util.class";
import { Loading, Message } from 'element-ui'
import Login from './../service/Login-service'
export default class ComApiHttp extends chatApiHttp{
  static LoadFlag = 0;
  constructor (){
    super()
  }
  static openApiAxios (params) {

    let _this = this;
    if(params.data){
      params['data']['appId'] = Config.appId;
      params['data']['userId'] = Utils.getSessionStorage('userId');
      params['data']['accessToken'] = Utils.getSessionStorage('accessToken');

    }
    params.data = this.filterNull(params.data);
      let _before = '';
      let _finally = '';
      let _beforeSuccess = '';
      let _success = '';
      let _error = '';
      if (params.before && params.before !== null) {
        _before = params.before
      }
      if (params.finally && params.finally !== null) {
        _finally = params.finally
      }
    if (params.success && params._uccess !== null) {
      _success = params.success
    }
      if (params.beforeSuccess && params.beforeSuccess !== null) {
        _beforeSuccess = params.beforeSuccess
      }
    if (params.error && params.error !== null) {
      _error = params.error
    }

     this.LoadFlag++;

     var loadinginstace = Loading.service();
      params.data = _this.setFormData(params.data);
      axios({
        method: params.method || 'POST',
        url: params.url,
        data: params.data,
        baseURL: Config.openApiUrl,
        withCredentials: false,
        timeout: 20000,
      }).then((res)=>{
        this.LoadFlag--;
        if(this.LoadFlag === 0){
          loadinginstace.close()
        }
        if (typeof _beforeSuccess === 'function') {
          _beforeSuccess(res.data.data)
        }
        if (res.data.successful) {
          _success(res.data)
        } else {
          //PP0008 PP0009 都直接取登录
           if(res.data.bizCode.code === 'PP0008' || res.data.bizCode.code === 'PP0009' ) {
             Login.goLogin();
             Message.error({
               message: res.data.bizCode.info
             })
          }else {
             if(_error){
               _error(res.data)
             }else{
               //弹出错误消息
               Message.error({
                 message:  res.data.bizCode.info
               })

             }
           }

        }
        if (typeof _finally === 'function') {
          _finally()
        }
      }).catch(function (err) {
        _this.LoadFlag = 0;
        if (err) {
          let message = err.message;
          if (message.indexOf('Network') > -1) {
            Message.error({
              message: '网络错误'
            })

          }
          if(message.indexOf('timeout') > -1){
            Message.error({
              message: '请求超时，系统异常'
            })

          }
          if(message.indexOf('404') > -1){
            Message.error({
              message: 'url路径错误'
            })
          }
        }
        loadinginstace.close()
      })
  }
}